package com.atguigu.yygh.hosp.listener;

import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.mq.MqConst;
import com.atguigu.yygh.mq.RabbitService;
import com.atguigu.yygh.vo.msm.MsmVo;
import com.atguigu.yygh.vo.order.OrderMqVo;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/*==========================================
      时 间:   2022-04-27
      讲 师:   刘  辉
      描 述:   尚硅谷教学团队
 ==========================================*/
@Component
public class OrderRabbitListener {

    @Autowired
    private ScheduleService  scheduleService;
    @Autowired
    private RabbitService rabbitService;

    @RabbitListener(bindings = {
            @QueueBinding(
                    exchange=@Exchange(name= MqConst.EXCHANGE_DIRECT_ORDER),
                    value = @Queue(name=MqConst.QUEUE_ORDER),
                    key=MqConst.ROUTING_ORDER
            )
    })
    public void consume(OrderMqVo orderMqVo, Message message, Channel channel){
        String scheduleId = orderMqVo.getScheduleId();
        Integer availableNumber = orderMqVo.getAvailableNumber();
        if(availableNumber != null){
            MsmVo msmVo = orderMqVo.getMsmVo();
            scheduleService.update(scheduleId,availableNumber);
        }else{
            scheduleService.cancel(scheduleId);
        }
        MsmVo msmVo = orderMqVo.getMsmVo();
        if(msmVo != null){
            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_MSM,MqConst.ROUTING_MSM_ITEM,msmVo);
        }

    }
}
